import prettier from "eslint-config-prettier";
import perfectionist from "eslint-plugin-perfectionist";
import prettierPlugin from "eslint-plugin-prettier";
import simpleImportSort from "eslint-plugin-simple-import-sort";
import sortKeysFix from "eslint-plugin-sort-keys-fix";
import vuePlugin from "eslint-plugin-vue";

import eslint from "@eslint/js";

// 共享的语言选项配置
const commonLanguageOptions = {
	ecmaVersion: "latest",
	sourceType: "module"
};
// 共享的浏览器全局变量
const browserGlobals = {
	Blob: "readonly",
	clearInterval: "readonly",
	clearTimeout: "readonly",
	console: "readonly",
	document: "readonly",
	Event: "readonly",
	EventTarget: "readonly",
	fetch: "readonly",
	File: "readonly",
	FormData: "readonly",
	Headers: "readonly",
	history: "readonly",
	localStorage: "readonly",
	location: "readonly",
	navigator: "readonly",
	Request: "readonly",
	Response: "readonly",
	sessionStorage: "readonly",
	setInterval: "readonly",
	setTimeout: "readonly",
	URL: "readonly",
	URLSearchParams: "readonly",
	WebSocket: "readonly",
	window: "readonly",
	XMLHttpRequest: "readonly"
};
// 共享的Node.js全局变量
const nodeGlobals = {
	__dirname: "readonly",
	__filename: "readonly",
	exports: "readonly",
	module: "readonly",
	process: "readonly",
	require: "readonly"
};
// 共享的未使用变量规则
const noUnusedVarsRule = [
	"error",
	{
		argsIgnorePattern: "^_",
		varsIgnorePattern: "^_"
	}
];
// 共享的排序规则配置
const sortingRules = {
	"perfectionist/sort-classes": [
		"error",
		{
			groups: [
				"static-property",
				"private-property",
				"property",
				"constructor",
				"static-method",
				"private-method",
				"method"
			],
			ignoreCase: true,
			order: "asc",
			type: "natural"
		}
	],
	"perfectionist/sort-exports": [
		"error",
		{
			ignoreCase: true,
			order: "asc",
			type: "alphabetical"
		}
	],
	"perfectionist/sort-imports": [
		"error",
		{
			ignoreCase: true,
			order: "asc",
			type: "natural"
		}
	],
	"perfectionist/sort-named-exports": [
		"error",
		{
			ignoreCase: true,
			order: "asc",
			type: "alphabetical"
		}
	],
	"perfectionist/sort-named-imports": [
		"error",
		{
			ignoreCase: true,
			order: "asc",
			type: "natural"
		}
	],
	"perfectionist/sort-objects": [
		"error",
		{
			ignoreCase: true,
			order: "asc",
			type: "natural"
		}
	],
	"perfectionist/sort-variable-declarations": [
		"error",
		{
			ignoreCase: true,
			order: "asc",
			type: "alphabetical"
		}
	]
};

export default [
	// 全局忽略文件
	{
		ignores: ["node_modules/", "dist/", "build/", "coverage/", "*.min.js", "*.d.ts", ".output/", ".APP/"]
	},
	// 基础 ESLint 规则
	eslint.configs.recommended,
	// Vue 插件配置（针对 Vue2）
	...vuePlugin.configs["flat/essential"],
	...vuePlugin.configs["flat/recommended"],
	// CommonJS 配置文件（.cjs）
	{
		files: ["**/*.cjs"],
		languageOptions: {
			...commonLanguageOptions,
			globals: nodeGlobals,
			sourceType: "commonjs"
		},
		rules: {
			"no-console": "off",
			"no-unused-vars": noUnusedVarsRule
		}
	},
	// 配置文件专用设置
	{
		files: ["rsbuild.config.js", "APP.config.js", "rollup.config.js", "webpack.config.js", "*.config.js"],
		languageOptions: {
			...commonLanguageOptions,
			globals: {
				...nodeGlobals,
				...browserGlobals
			}
		},
		rules: {
			"no-console": "off",
			"no-unused-vars": noUnusedVarsRule
		}
	},
	// JS文件通用配置
	{
		files: ["**/*.js"],
		languageOptions: {
			...commonLanguageOptions,
			globals: browserGlobals
		},
		plugins: {
			perfectionist,
			prettier: prettierPlugin,
			"simple-import-sort": simpleImportSort
		},
		rules: {
			// 代码风格规则
			"array-bracket-spacing": ["error", "never"],
			// 基础代码质量规则
			curly: ["error", "all"],
			eqeqeq: ["error", "always"],
			"no-console": "warn",
			"no-debugger": "warn",
			"no-unused-vars": noUnusedVarsRule,
			"no-var": "error",
			"object-curly-spacing": ["error", "always"],
			// 排序规则（禁用 perfectionist/sort-imports 避免与 simple-import-sort 冲突）
			"perfectionist/sort-classes": sortingRules["perfectionist/sort-classes"],
			"perfectionist/sort-exports": sortingRules["perfectionist/sort-exports"],
			"perfectionist/sort-named-exports": sortingRules["perfectionist/sort-named-exports"],
			"perfectionist/sort-named-imports": sortingRules["perfectionist/sort-named-imports"],
			"perfectionist/sort-objects": sortingRules["perfectionist/sort-objects"],
			"perfectionist/sort-variable-declarations": sortingRules["perfectionist/sort-variable-declarations"],
			"prefer-const": "error",
			// Prettier 和导入排序
			"prettier/prettier": ["error", { endOfLine: "auto" }],
			"quote-props": ["error", "as-needed"],
			"simple-import-sort/exports": "error",
			"simple-import-sort/imports": [
				"error",
				{
					groups: [
						// Vue 官方组件优先
						["^vue$", "^vue-router$", "^vuex$", "^@vue/"],
						// 其他第三方库
						["^[^@.]"],
						// @ 开头的内部模块
						["^@/"],
						// 相对路径导入
						["^\\."],
						// CSS 文件单独分组
						["\\.(css|scss|sass|less|styl)$"]
					]
				}
			]
		}
	},
	// 源代码文件特殊配置
	{
		files: ["src/**/*.js"],
		rules: {
			"no-alert": "error",
			// 源代码中更严格的规则
			"no-console": "warn",
			"no-debugger": "error",
			"no-eval": "error",
			"no-implied-eval": "error",
			"prefer-template": "error"
		}
	},
	// Vue 文件配置
	{
		files: ["**/*.vue"],
		languageOptions: {
			...commonLanguageOptions,
			globals: browserGlobals
		},
		plugins: {
			perfectionist,
			prettier: prettierPlugin,
			"simple-import-sort": simpleImportSort,
			"sort-keys-fix": sortKeysFix
		},
		rules: {
			"no-console": "warn",
			"no-debugger": "warn",
			// 禁用 perfectionist/sort-imports 避免与 simple-import-sort 冲突
			"perfectionist/sort-named-imports": [
				"error",
				{
					ignoreCase: true,
					order: "asc",
					type: "natural"
				}
			],
			// 禁用可能与Vue组件选项冲突的规则
			"perfectionist/sort-objects": "off",
			// Prettier
			"prettier/prettier": "error",
			"simple-import-sort/exports": "error",
			// 使用 simple-import-sort 替代 perfectionist/sort-imports
			"simple-import-sort/imports": [
				"error",
				{
					groups: [
						// Vue 官方组件优先
						["^vue$", "^vue-router$", "^vuex$", "^@vue/"],
						// 其他第三方库
						["^[^@.]"],
						// @ 开头的内部模块
						["^@/"],
						// 相对路径导入
						["^\\."],
						// CSS 文件单独分组
						["\\.(css|scss|sass|less|styl)$"]
					]
				}
			],
			"sort-keys-fix/sort-keys-fix": "off",
			// Vue特有规则
			"vue/attributes-order": [
				"error",
				{
					alphabetical: false,
					order: [
						"DEFINITION",
						"LIST_RENDERING",
						"CONDITIONALS",
						"RENDER_MODIFIERS",
						"GLOBAL",
						"UNIQUE",
						"TWO_WAY_BINDING",
						"OTHER_DIRECTIVES",
						"OTHER_ATTR",
						"EVENTS",
						"CONTENT"
					]
				}
			],
			"vue/component-definition-name-casing": ["error", "PascalCase"],
			"vue/component-name-in-template-casing": ["error", "kebab-case"],
			"vue/html-self-closing": [
				"error",
				{
					html: {
						component: "always",
						normal: "always",
						void: "always"
					},
					math: "always",
					svg: "always"
				}
			],
			"vue/max-attributes-per-line": [
				"error",
				{
					multiline: 1,
					singleline: 3
				}
			],
			"vue/multi-word-component-names": "off",
			"vue/no-v-html": "warn",
			"vue/order-in-components": [
				"error",
				{
					order: [
						"el",
						"name",
						"parent",
						"functional",
						["delimiters", "comments"],
						["components", "directives", "filters"],
						"extends",
						"mixins",
						"inheritAttrs",
						"model",
						["props", "propsData"],
						"data",
						"computed",
						"watch",
						// 生命周期钩子按执行顺序排列
						"beforeCreate",
						"created",
						"beforeMount",
						"mounted",
						"beforeUpdate",
						"updated",
						"activated",
						"deactivated",
						"beforeUnmount",
						"unmounted",
						"beforeDestroy",
						"destroyed",
						"errorCaptured",
						// 方法放在最后
						"methods",
						["template", "render"],
						"renderError"
					]
				}
			],
			"vue/prop-name-casing": ["error", "camelCase"],
			"vue/require-default-prop": "error",
			"vue/require-prop-types": "error",
			"vue/v-bind-style": ["error", "shorthand"],
			"vue/v-on-style": ["error", "shorthand"]
		}
	},
	// 测试文件配置
	{
		files: ["**/*.test.js", "**/*.spec.js", "**/tests/**/*.js", "**/__tests__/**/*.js"],
		rules: {
			"no-console": "off",
			"no-unused-expressions": "off"
		}
	},
	// 禁用与 Prettier 冲突的 ESLint 规则（必须放在最后）
	prettier
];
